package com.htjc.integral.conversion.inner.service.impl;

import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.htjc.integral.common.util.JsonUtil;
import com.htjc.integral.common.util.StrUtil;
import com.htjc.integral.conversion.inner.mapper.EcExtJfWsdlMapper;
import com.htjc.integral.conversion.inner.service.InvokeBuildService;
import com.htjc.integral.conversion.inner.util.ResultModel;
import com.htjc.integral.conversion.inner.util.SoaClient;
import com.htjc.integral.conversion.inner.util.XflUtil;
import com.htjc.integral.conversion.inner.util.XmlConverUtil;
import com.htjc.integral.conversion.inner.util.enums.ParamsEnum;
import com.htjc.integral.model.conversion.EcExtJfWsdl;

import lombok.extern.slf4j.Slf4j;

@Slf4j(topic = "jfLog")
@Service
public class InvokeBuildServiceImpl implements InvokeBuildService {
	@Autowired
	private EcExtJfWsdlMapper wsdlMapper;

	@Override
	public String queryIntegral(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		log.info("调用自建网省查询方法开始=====》参数：{}", jsonObject);
		String result = null;
		try {
			log.info("---------自建网省查询方法---------");
			String provinceCode = jsonObject.getString("ORGNO");
			log.info("---------自建网省查询方法---------,请求网省={}", provinceCode);
			if (ParamsEnum.ZHEJIANG_LONG.getParamValue().equalsIgnoreCase(provinceCode)
					|| ParamsEnum.ZHEJIANG_SHORT.getParamValue().equalsIgnoreCase(provinceCode)) {
				log.info("进入浙江自建网省积分查询接口====>");
				Map<String, Object> map = queryScoreDetailZheJiang(jsonObject);
				JSONObject json = JSONObject.parseObject(JsonUtil.bean2json(ResultModel.AccountReturnSucceed(map)));
				log.info("进入浙江自建网省积分查询接口====>返回结果={}", map.toString());
			}

		} catch (Exception e) {
			// TODO: handle exception
			log.error("调用自建网省查询方法异常=====》错误信息：{}", e.getMessage());
		}

		log.info("调用自建网省查询方法结束=====》参数：{}", result);
		return result;
	}

	@Override
	public String conversionIntegral(JSONObject jsonObject) {
		// TODO Auto-generated method stub
		log.info("调用自建网省方法开始=====》参数：{}", jsonObject);
		String result = null;
		try {

		} catch (Exception e) {
			// TODO: handle exception
			log.error("调用自建网省方法异常=====》错误信息：{}", e.getMessage());
		}

		log.info("调用自建网省方法结束=====》参数：{}", result);
		return result;
	}

	/**
	 * 调用自建网省方法
	 */
	@Override
	public String doInvoke(JSONObject jsonObject) {
		log.info("调用自建网省方法开始=====》参数：{}", jsonObject);
		String result = null;
		try {

		} catch (Exception e) {
			// TODO: handle exception
			log.error("调用自建网省方法异常=====》错误信息：{}", e.getMessage());
		}

		log.info("调用自建网省方法结束=====》参数：{}", result);
		return result;
	}

	/**
	 * 浙江查询接口
	 * 
	 * @param jsonObject
	 * @return
	 */
	public Map<String, Object> queryScoreDetailZheJiang(JSONObject jsonObject) {
		log.info("调用浙江自建网省查询方法开始=====》参数：{}", jsonObject);
		String result = null;
		String inputXml = "";
		Map<String, Object> mReturn = new HashMap<String, Object>();
		try {

			String consNo = jsonObject.getString("CONSNO");
			String userId = jsonObject.getString("USERID");
			String orgNo = jsonObject.getString("ORGNO"); // 短码
			String deOrgNo = orgNo; // 长码
			String loginAccount = jsonObject.getString("LOGIN_ACCOUNT");
			log.info("---------浙江自建网省查询方法---------,用户ID={},网省编码={},户号={}", userId, orgNo, consNo);
			// 根据户号 和供电单位获取用户信息
			EcExtJfWsdl jfWsdl = wsdlMapper.getJfWsdl(deOrgNo, "jfmxcx");
			log.error("queryScoreDetail接口请求配置信息：" + jfWsdl.toString());
			String wsdlurl = jfWsdl.getWsdlAddress();// 请求wsdlurl
			String wsdlHeader = jfWsdl.getZylzd1();// 请求头
			String acctId = userId;
			String userName = loginAccount;
			/*
			 * inputXml
			 * ="<ORDER><srvCode>0223003</srvCode><channelCode>ESGCC</channelCode><acctId>"
			 * +acctId+"</acctId><userName>"+userName+"</userName><orgNo>" +
			 * orgNo + "</orgNo>" + "<consNo>"+ consNo +"</consNo></ORDER>";
			 */
			inputXml = "<DBSET><R><C N=\"srvCode\">LSSP_000180</C>" + "<C N=\"channelCode\">ESGCC</C><C N=\"acctId\">"
					+ acctId + "</C><C N=\"userName\">" + userName + "</C><C N=\"consNo\">" + consNo
					+ "</C><C N=\"orgNo\">" + orgNo + "</C></R></DBSET>";

			String input = "<![CDATA[?]]>".replace("?", inputXml);
			log.error("********queryScoreDetail方法请求拼接的参数xml：" + input + ";");
			// 返回结果的result为空 应该是营销的测试接口有问题
			Map<String, String> map = SoaClient.requestSoa(wsdlurl, wsdlHeader, input, "?", "", "UTF-8",
					jfWsdl.getZylzd2());

			String success = "false";
			if (map.get("success") != null) {
				success = map.get("success");
			}
			if (map.get("result") != null) {
				result = map.get("result");
			}
			log.error("queryScoreDetail方法调用dxp后返回的结果：" + result);

			if (result != null && success.equals("true")) {
				if ("330000".equals(deOrgNo)) {
					try {
						List<Map> listr = XflUtil.toList("<DBSET><R>" + result + "</R></DBSET>");
						if (listr.size() > 0) {
							mReturn = listr.get(0);
							// 处理如果的rtnCode的返回值0表示成功，1表失败
							if (mReturn.containsKey("rtnCode")) {
								String rtnCode = String.valueOf(mReturn.get("rtnCode"));
								if ("0".equals(rtnCode)) {
									mReturn.put("code", "1");
								} else if ("1".equals(rtnCode)) {
									mReturn.put("code", "0");
								}
							}
							// 处理积分返回结果，去掉小数
							if (mReturn.containsKey("score")) {
								String score = String.valueOf(mReturn.get("score"));
								score = score.indexOf(".") > -1 ? score.substring(0, score.indexOf(".")) : score;
								if ("".equals(score) || null == score) {
									score = "0";
								}
								mReturn.put("score", score);
							}
						}
					} catch (Exception e) {
						// TODO Auto-generated catch block
						log.error("浙江网省查询接口异常 错误信息={}", e.getMessage());
					}
				}
				mReturn.put("orgNo", deOrgNo);

				log.info("XmlConverUtil.xmltoMap2(result)--mReturn:", mReturn.toString());
			}
		} catch (Exception e) {
			// TODO: handle exception
			log.error("调用自建网省方法异常=====》错误信息：{}", e.getMessage());
		}

		log.info("调用自建网省方法结束=====》参数：{}", mReturn.toString());
		return mReturn;
	}

	/**
	 * 浙江转化接口
	 * 
	 * @param jsonObject
	 * @return
	 */
	public Map<String, Object> conversionScoreDetailZheJiang(JSONObject jsonObject) {

		log.info("调用浙江自建网省查询方法开始=====》参数：{}", jsonObject);
		String acctId = null;
		String userName = null;
		String consNo = null;
		String deOrgNo = null;
		String orgNo = null;
		String score = null;
		String goldCoin = null;
		String convertTime = null;
		String id = null;
		String result = null;
		String inputXml = "";
		String input = "";
		Map<String, Object> mReturn = new HashMap<String, Object>();
		Map<String, String> map = new HashMap<String, String>();
		String success = "false";

		try {

//			1、获取网省的短码///////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			//////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////////
			
			
			EcExtJfWsdl jfWsdl = this.wsdlMapper.getJfWsdl(deOrgNo, "jfzh");
			if (!StrUtil.isEmpty(jfWsdl)) {

				log.info("queryScoreConvert接口请求配置信息：" + jfWsdl.toString());
				String wsdlurl = jfWsdl.getWsdlAddress();// 请求wsdlurl
				String wsdlHeader = jfWsdl.getZylzd1();// 请求头
				// 请求WSDL
				log.info("********请求WebServicel...START：" + new Date() + ";");
				// 浙江的请求参数与别的网省不一样，单独处理
//				acctId = String.valueOf(usmap.get("PARTNERID"));
//				userName = String.valueOf(usmap.get("LOGIN_ACCOUNT"));
				userName = userName.length() > 256 ? userName.substring(0, 256) : userName;
				
				inputXml = "<DBSET><R><C N=\"srvCode\">LSSP_000181</C>"
						+ "<C N=\"channelCode\">ESGCC</C><C N=\"acctId\">" + acctId + "</C><C N=\"userName\">"
						+ userName + "</C><C N=\"consNo\">" + consNo + "</C><C N=\"orgNo\">" + orgNo + "</C>"
						+ "<C N=\"score\">" + score + "</C><C N=\"goldCoin\">" + goldCoin + "</C><C N=\"convertTime\">"
						+ convertTime + "</C><C N=\"id\">" + id + "</C>" + "</R></DBSET>";
				input = "<![CDATA[?]]>".replace("?", inputXml);
				log.info("********queryScoreConvert方法请求拼接的参数xml={}", input + ";");
				map = SoaClient.requestSoa(wsdlurl, wsdlHeader, input, "?", "", "UTF-8", jfWsdl.getZylzd2());
				if (map.get("success") != null) {
					success = map.get("success");
				}
				if (map.get("result") != null) {
					result = map.get("result");
				}

				log.info("queryScoreConvert方法调用dxp后返回的结果={}", result);
				if (!StrUtil.isEmpty(result) && success.equals("true")) {
					List<Map> listr = XflUtil.toList("<DBSET><R>" + result + "</R></DBSET>");
					if (listr.size() > 0) {
						mReturn = listr.get(0);
						// 处理如果的rtnCode的返回值0表示成功，1表失败
						if (mReturn.containsKey("rtnCode")) {
							String rtnCode = String.valueOf(mReturn.get("rtnCode"));
							if ("0".equals(rtnCode)) {
								mReturn.put("code", "1");
							} else if ("1".equals(rtnCode)) {
								mReturn.put("code", "0");
							}
						}
						// 处理积分返回结果，去掉小数
						if (mReturn.containsKey("score")) {
							String score1 = String.valueOf(mReturn.get("score"));
							score1 = score1.indexOf(".") > -1 ? score1.substring(0, score1.indexOf(".")) : score1;
							if ("".equals(score1) || null == score1) {
								score1 = "0";
							}
							mReturn.put("score", score1);
						}
					}
				}

			}

		} catch (Exception e) {
			// TODO: handle exception
			log.error("调用自建网省方法异常=====》错误信息：{}", e.getMessage());
		}

		log.error("conversion-Inner中的ScoreConvert方法处理后返回结果map:" + mReturn.toString());

		mReturn.put("fromxml", map.get("fromxml"));
		mReturn.put("toxml", map.get("toxml"));
		return mReturn;

	}

}
